<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类别名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入类别名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别编码" prop="typeCode">
        <el-input
          v-model="queryParams.typeCode"
          placeholder="请输入类别编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:productConfig:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="productConfigList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      max-height="550"
    >
      <el-table-column label="类别名称" prop="typeName" />
      <el-table-column label="类别编码" align="center" prop="typeCode" />
<!--      <el-table-column label="显示顺序" align="center" prop="orderNum" />-->
      <el-table-column label="状态" align="center" prop="status"  :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:productConfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['equipment:productConfig:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:productConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改物资类型管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别名称" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输入类别名称"  :disabled="openDisabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别编码" prop="typeCode">
              <el-input v-model="form.typeCode" placeholder="请输入类别编码"  :disabled="openDisabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上级节点" prop="parentId">
              <treeselect v-model="form.parentId" :options="productConfigOptions" :normalizer="normalizer" placeholder="请选择上级节点"  :disabled="openDisabled"/>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="显示顺序" prop="orderNum">-->
<!--              <el-input v-model="form.orderNum" placeholder="请输入显示顺序"  :disabled="openDisabled"/>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="状态">
              <el-select v-model="form.status" placeholder="请选择类型" :disabled="openDisabled">
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer"  v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listProductConfig, getProductConfig, delProductConfig, addProductConfig, updateProductConfig, exportProductConfig } from "@/api/equipment/materials/productConfig";
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";

    export default {
        name: "ProductConfig",
        components: {
            Treeselect
        },
        data() {
            var validateCode = (rule, value, callback) => {
                if (value === ''&&value===null) {
                    callback(new Error('不能为空'));
                } else {
                    let qParams={
                        typeCode:value,
                        // status:0
                    };
                    listProductConfig(qParams).then(response => {
                        let code=1;
                        for(let i=0;i<response.data.length;i++){
                            if(response.data[i].id!=this.form.id){
                                code++;
                            }
                        }
                        if(code>1){
                            callback(new Error('类型编码重复!'));
                        }else {
                            callback();
                        }
                    })
                }
            };
            return {
                //弹窗是否禁止编辑
                openDisabled: true,
                // 遮罩层
                loading: true,
                // 显示搜索条件
                showSearch: true,
                // 物资类型管理表格数据
                productConfigList: [],
                // 物资类型管理树选项
                productConfigOptions: [],
                statusOptions:[
                    { dictLabel: "正常", dictValue: "0" },
                    { dictLabel: "停用", dictValue: "1" },
                ],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    typeName: null,
                    typeCode: null,
                    parentId: null,
                    ancestors: null,
                    orderNum: null,
                    level: null,
                    ownerGroup: null,
                    status: null,
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    typeCode: [
                                  { required: true, message: "不能为空", trigger: "blur" },
                                  { validator: validateCode, message: "类型编码重复", trigger: "blur" },
                              ],
                    typeName: [{ required: true, message: "不能为空", trigger: "blur" }],
                }
            };
        },
        created() {
            this.getList();
        },
        methods: {
            // 类型字典翻译
            statusFormat(row, column) {
                return this.selectDictLabel(this.statusOptions, row.status);
            },
            /** 查询物资类型管理列表 */
            getList() {
                this.loading = true;
                listProductConfig(this.queryParams).then(response => {
                    this.productConfigList = this.handleTree(response.data, "id", "parentId");
                    this.loading = false;
                });
            },
            /** 转换物资类型管理数据结构 */
            normalizer(node) {
                if (node.children && !node.children.length) {
                    delete node.children;
                }
                return {
                    id: node.id,
                    label: node.typeName,
                    children: node.children
                };
            },
            /** 查询物资类型管理下拉树结构 */
            getTreeselect() {
                listProductConfig().then(response => {
                    this.productConfigOptions = [];
                    const data = { id: 0, typeName: '顶级节点', children: [] };
                    data.children = this.handleTree(response.data, "id", "parentId");
                    this.productConfigOptions.push(data);
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    typeName: null,
                    typeCode: null,
                    parentId: null,
                    ancestors: null,
                    orderNum: null,
                    level: null,
                    ownerGroup: null,
                    status: "0",
                    delFlag: null,
                    remark: null,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 新增按钮操作 */
            handleAdd(row) {
                this.reset();
                this.openDisabled = false;
                this.getTreeselect();
                if (row != null && row.id) {
                    this.form.parentId = row.id;
                } else {
                    this.form.parentId = 0;
                }
                this.open = true;
                this.title = "添加物资类型管理";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                this.openDisabled = false;
                this.getTreeselect();
                if (row != null) {
                    this.form.parentId = row.id;
                }
                getProductConfig(row.id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改物资类型管理";
                });
            },
            /** 修改按钮操作 */
            handleView(row) {
                this.reset();
                this.openDisabled = true;
                this.getTreeselect();
                if (row != null) {
                    this.form.parentId = row.id;
                }
                getProductConfig(row.id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "查看物资类型管理";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateProductConfig(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addProductConfig(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                this.$confirm('是否确认删除物资类型管理编号为"' + row.id + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return delProductConfig(row.id);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                }).catch(() => {});
            }
        }
    };
</script>
